import { defineStore } from 'pinia';
import { RouteRecordRaw } from 'vue-router';
import { nextTick } from 'vue';
import { getCurrentRoute } from '@/router';
import type { TRouterInfo, TTabRouterType } from '@/types/interface';
import { emit } from '@/soar';
import { useSettingStore } from '@/store';

const homeRoute: Array<TRouterInfo> = [
  {
    path: '/dashboard/base',
    routeIdx: 0,
    title: '仪表盘',
    name: 'DashboardBase',
    isHome: true,
    key: Math.random(),
  },
];

const state = {
  tabRouterList: homeRoute,
};

export const useTabsRouterStore = defineStore('tabsRouter', {
  state: () => state,
  getters: {
    tabRouters: (state: TTabRouterType) => state.tabRouterList,
  },
  actions: {
    setTitle(title: string) {
      const route = getCurrentRoute().fullPath;
      const index = this.tabRouters.findIndex((x) => x.path === route);
      if (index === -1) {
        return;
      }
      this.tabRouters[index].title = title;
      emit('updateTitle', title);
    },
    refresh(route?: string | RouteRecordRaw) {
      if (!route) {
        // @ts-ignore
        route = getCurrentRoute();
      }
      // @ts-ignore
      if (route.fullPath) {
        // @ts-ignore
        route = route.fullPath;
      }
      const index = this.tabRouters.findIndex((x) => x.path === route);
      if (index === -1) {
        return;
      }
      this.toggleTabRouterAlive(index);
      nextTick(() => {
        this.toggleTabRouterAlive(index);
      });
    },
    // 处理刷新
    toggleTabRouterAlive(routeIdx: number | string) {
      if (this.tabRouters[routeIdx]) {
        this.tabRouters[routeIdx].key = Math.random();
      }
    },
    // 处理新增
    appendTabRouterList(newRoute: TRouterInfo) {
      if (this.tabRouters.findIndex((route: TRouterInfo) => route.path === newRoute.path) === -1) {
        if (!useSettingStore().isUseTabsRouter) {
          this.tabRouterList.splice(1, this.tabRouterList.length - 1);
        }
        this.tabRouterList.push(newRoute);
      }
    },
    // 处理关闭当前
    closeCurrentTabRouter(path: string) {
      const routeIdx = this.tabRouterList.findIndex((x) => x.path === path);
      if (routeIdx === -1) {
        return false;
      }
      this.tabRouterList = this.tabRouterList.slice(0, routeIdx).concat(this.tabRouterList.slice(routeIdx + 1));
      return true;
    },
    // 处理关闭当前
    subtractCurrentTabRouter(newRoute: TRouterInfo) {
      const { routeIdx } = newRoute;
      this.tabRouterList = this.tabRouterList.slice(0, routeIdx).concat(this.tabRouterList.slice(routeIdx + 1));
    },
    // 处理关闭右侧
    subtractTabRouterBehind(newRoute: TRouterInfo) {
      const { routeIdx } = newRoute;
      this.tabRouterList = this.tabRouterList.slice(0, routeIdx + 1);
    },
    // 处理关闭左侧
    subtractTabRouterAhead(newRoute: TRouterInfo) {
      const { routeIdx } = newRoute;
      this.tabRouterList = homeRoute.concat(this.tabRouterList.slice(routeIdx));
    },
    // 处理关闭其他
    subtractTabRouterOther(newRoute: TRouterInfo) {
      const { routeIdx } = newRoute;
      this.tabRouterList = routeIdx === 0 ? homeRoute : homeRoute.concat([this.tabRouterList?.[routeIdx]]);
    },
    removeTabRouterList() {
      this.tabRouterList = [];
    },
    initTabRouterList(newRoutes: TRouterInfo[]) {
      newRoutes?.forEach((route: TRouterInfo) => this.appendTabRouterList(route));
    },
  },
});
